package sfin.swt.composite;

import java.util.ResourceBundle;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.ColorDialog;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Text;

import sfin.controller.ConfigController;

public class ConfigComposite extends Composite{

	private ResourceBundle bundle;
	private TabFolder folder;

	private SelectedColorGetter[] colorGetters = new SelectedColorGetter[18];

	private ConfigController controller;
	private Button chkInitContaPadrao;
	private Button[] dtPgConfigButtons;
	
	public ConfigComposite(Composite parent, int style, ResourceBundle bundle, ConfigController configController) {
		super(parent, style);
		this.controller = configController;
		this.bundle = bundle;
		initializeBackgroundGetters();
		fillColorData();
		initialize();
		fillConfigData();
		pack();
	}

	private void initializeBackgroundGetters(){
		for (int i = 0; i < colorGetters.length; i++) {
			colorGetters[i] = new SelectedColorGetter();
		}
	}

	private void fillColorData(){

		int[][][] colors = controller.getBackgrounds();

		for (int i = 0; i < colors.length; i++) {
			colorGetters[i].setRGB(colors[i][0]).setDefaultRGB(colors[i][1]);
		}

	}

	private void fillConfigData(){
		chkInitContaPadrao.setSelection(controller.isIniContaPadrao());
	}

	private void fillDtPgtoButtonsData(Group group){

		String[] labels = controller.getDtPgtoLabels();
		dtPgConfigButtons = new Button[labels.length];
		int selected = controller.getDtPgtoIndex();

		for (int i = 0; i < labels.length; i++) {
			Button button = new Button(group, SWT.RADIO);
			button.setLayoutData(new RowData());
			button.setText(labels[i]);
			button.setSelection( selected == i);
			dtPgConfigButtons[i] = button;
		}

	}

	private void initialize() {
		setLayout(new GridLayout());               
		folder = new TabFolder(this, SWT.NONE);
		createIniContents();
		createDataContents();
		createColorsContents();
	}

	protected void createIniContents(){

		GridData gridData = new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL);
		folder.setLayoutData(gridData);

		TabItem tabItem = new TabItem(folder, SWT.NONE);

		Composite composite = new Composite(folder, SWT.NONE);
		composite.setLayout(new GridLayout());

		tabItem.setControl(composite);
		tabItem.setText(bundle.getString("preferences.init.text"));

		composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

		Group group = new Group(composite, SWT.NONE);
		RowLayout rowLayout = new RowLayout(SWT.VERTICAL);
		rowLayout.fill = true;
		group.setLayout(rowLayout);
		group.setText(bundle.getString("preferences.init.text"));
		
		group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

		Group innerGroup = new Group(group, SWT.NONE);
		innerGroup.setText(bundle.getString("string.general.account"));
		innerGroup.setLayout(new RowLayout());
		innerGroup.setLayoutData(new RowData());

		chkInitContaPadrao = new Button(innerGroup, SWT.CHECK);
		chkInitContaPadrao.setText(bundle.getString("preferences.account.label.open-default"));
		chkInitContaPadrao.setLayoutData(new RowData());

		innerGroup = new Group(group, SWT.NONE);
		innerGroup.setText(bundle.getString("preferences.entry.label.dtpg.title"));
		innerGroup.setLayout(new RowLayout(SWT.VERTICAL));

		innerGroup.setLayoutData(new RowData());
		
		fillDtPgtoButtonsData(innerGroup);
		
		

	}
	
	protected void createDataContents(){

		GridData gridData = new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL);
		folder.setLayoutData(gridData);

		TabItem tabItem = new TabItem(folder, SWT.NONE);

		Composite composite = new Composite(folder, SWT.NONE);
		composite.setLayout(new GridLayout());

		tabItem.setControl(composite);
		tabItem.setText(bundle.getString("preferences.config.data.title"));

		composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		
		Group group = new Group(composite, SWT.NONE);
		group.setText(bundle.getString("preferences.config.data.text"));
		group.setLayout(new GridLayout(2,false));
		group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		
		Label label = new Label(group, SWT.NONE);
		label.setText(bundle.getString("preferences.config.data.label"));
		
		gridData = new GridData();
		label.setLayoutData(gridData);
		
		Text text = new Text(group, SWT.BORDER | SWT.READ_ONLY);
//		text.setText(controller.getDataLocation());
		
		text.setBackground(new Color(getDisplay(), new RGB(242, 241, 240)));
		
		gridData = new GridData();
		text.setLayoutData(gridData);

		label.pack();
		text.pack();

		
		
	}

	protected void createColorsContents(){

		TabItem tabItem = new TabItem(folder, SWT.NONE);
		Composite composite = new Composite(folder, SWT.NONE);
		composite.setLayout(new GridLayout());

		tabItem.setControl(composite);
		tabItem.setText(bundle.getString("preferences.color-schema.title"));

		GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
		composite.setLayoutData(gridData);

		Group colorGroup = new Group(composite, SWT.NONE);
		colorGroup.setText(bundle.getString("preferences.entry.label.colors.title"));
		colorGroup.setLayout(new GridLayout(2,true));

		gridData = new GridData();
		colorGroup.setLayoutData(gridData);

		Group group = new Group(colorGroup, SWT.NONE);
		group.setText(bundle.getString("grupo.lancamento.receita.name"));
		group.setLayout(new GridLayout(4,false));
		group.setLayoutData(new GridData());

		createColorContent(group, "preferences.entry.label.paid-color", colorGetters[0]);
		createColorContent(group, "preferences.entry.label.text-paid-color", colorGetters[1],true);

		createColorContent(group, "preferences.entry.label.not-paid-color", colorGetters[2]);
		createColorContent(group, "preferences.entry.label.text-not-paid-color", colorGetters[3],true);

		createColorContent(group, "preferences.entry.label.task-color", colorGetters[4]);
		createColorContent(group, "preferences.entry.label.text-task-color", colorGetters[5]);

		group = new Group(colorGroup, SWT.NONE);
		group.setText(bundle.getString("string.general.expense"));
		group.setLayout(new GridLayout(4,false));
		group.setLayoutData(new GridData());

		createColorContent(group, "preferences.entry.label.paid-color", colorGetters[6]);
		createColorContent(group, "preferences.entry.label.text-paid-color", colorGetters[7],true);

		createColorContent(group, "preferences.entry.label.not-paid-color", colorGetters[8]);
		createColorContent(group, "preferences.entry.label.text-not-paid-color", colorGetters[9],true);

		createColorContent(group, "preferences.entry.label.task-color", colorGetters[10]);
		createColorContent(group, "preferences.entry.label.text-task-color", colorGetters[11]);


		group = new Group(colorGroup, SWT.NONE);
		group.setText(bundle.getString("grupo.lancamento.investimento.name"));
		group.setLayout(new GridLayout(4,false));
		group.setLayoutData(new GridData());

		createColorContent(group, "preferences.entry.label.paid-color", colorGetters[12]);
		createColorContent(group, "preferences.entry.label.text-paid-color", colorGetters[13],true);

		createColorContent(group, "preferences.entry.label.not-paid-color", colorGetters[14]);
		createColorContent(group, "preferences.entry.label.text-not-paid-color", colorGetters[15],true);

		createColorContent(group, "preferences.entry.label.task-color", colorGetters[16]);
		createColorContent(group, "preferences.entry.label.text-task-color", colorGetters[17]);

		
		
		
		Composite actionComposite = new Composite(this, SWT.RIGHT_TO_LEFT);
		RowLayout rowLayout = new RowLayout(SWT.HORIZONTAL);
		rowLayout.pack = false;
		actionComposite.setLayout(rowLayout);

		gridData = new GridData(GridData.FILL_HORIZONTAL);
		actionComposite.setLayoutData(gridData);
		
		Button actionButton = new Button(actionComposite, SWT.PUSH);
		actionButton.setText(bundle.getString("string.general.ok"));
		actionButton.setFocus();
		actionButton.setLayoutData(new RowData());
		actionButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				getParent().dispose();
			}
		});

		actionButton = new Button(actionComposite, SWT.PUSH);
		actionButton.setText(bundle.getString("string.general.apply"));

		actionButton.setLayoutData(new RowData());

		actionButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				int[][][] backs = new int[colorGetters.length][2][3];
				for (int i = 0; i < backs.length; i++) {
					backs[i][0] = colorGetters[i].getBackgroundValue();
					backs[i][1] = colorGetters[i].getDefaultBackgroundValue();
				}

				controller.setBackgrounds(backs);
				controller.setIniContaPadrao(chkInitContaPadrao.getSelection());
				
				for(int i = 0; i<dtPgConfigButtons.length; i++){
					if(dtPgConfigButtons[i].getSelection()){
						controller.setDtPgtoIndex(i);
					}
				}
				controller.callSave();
			}
		});

		
		
	}

	private void createColorContent(Group group, String labelText, final SelectedColorGetter getter){
		createColorContent(group, labelText, getter,false);
	}

	private void createColorContent(Group group, String labelText, final SelectedColorGetter getter, boolean createSeparator){

		Label label = new Label(group, SWT.NONE);
		label.setText(bundle.getString(labelText));

		GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
		gridData.grabExcessHorizontalSpace = true;
		gridData.horizontalAlignment = GridData.END;
		label.setLayoutData(gridData);

		final Label colorLabel = new Label(group, SWT.BORDER);

		gridData = new GridData();
		gridData.widthHint = 70;
		gridData.grabExcessHorizontalSpace = true;
		colorLabel.setLayoutData(gridData);

		colorLabel.setBackground(new Color(getDisplay(),getter.background));
		getter.setLabel(colorLabel);

		Button button = new Button(group, SWT.PUSH);
		button.setText(bundle.getString("string.general.choose"));
		button.addSelectionListener(getter);

		gridData = new GridData();
		button.setLayoutData(gridData);

		button = new Button(group, SWT.PUSH);
		button.setText(bundle.getString("string.general.default"));
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				getter.reset();
			}
		});

		gridData = new GridData();
		button.setLayoutData(gridData);

		if(createSeparator){
			Label separator = new Label(group, SWT.SEPARATOR | SWT.HORIZONTAL);
			gridData = new GridData(GridData.FILL_HORIZONTAL);
			gridData.horizontalSpan = 4;
			separator.setLayoutData(gridData);
		}

	}

	public Shell getCompositeShell(){
		return getShell();
	}

	public Display getCompositeDisplay(){
		return getDisplay();
	}

	class SelectedColorGetter implements SelectionListener, MouseListener{

		private RGB background;
		private RGB defaultBackground;
		private Label backgroundLabel;

		public SelectedColorGetter setRGB(int[] back){
			this.background = new RGB(back[0],back[1],back[2]);
			return this;
		}

		public SelectedColorGetter setDefaultRGB(int[] back){
			this.defaultBackground = new RGB(back[0],back[1],back[2]);
			return this;
		}

		@Override
		public void widgetSelected(SelectionEvent e) {
			selectColor();
		}

		public int[] getBackgroundValue(){
			return new int[]{background.red,background.green,background.blue};
		}

		public int[] getDefaultBackgroundValue(){
			return new int[]{defaultBackground.red, defaultBackground.green, defaultBackground.blue};
		}

		public void setLabel(final Label label){
			if(label == null)
				throw new IllegalArgumentException("label cannot be null.");

			label.addMouseListener(this);
			this.backgroundLabel = label;
		}

		protected void selectColor(){
			ColorDialog colorDialog = new ColorDialog(getCompositeShell());
			colorDialog.setRGB(background);

			RGB rgbColor = colorDialog.open();
			if(rgbColor != null){
				background = rgbColor;
				updateLabel();	
			}
		}

		public void reset(){
			this.background = this.defaultBackground;
			updateLabel();
		}

		private void updateLabel(){
			backgroundLabel.setBackground(new Color(getCompositeDisplay(), background));
		}

		@Override
		public void widgetDefaultSelected(SelectionEvent e) {
		}

		public RGB getRGB(){
			return background;
		}

		@Override
		public void mouseDoubleClick(MouseEvent e) {
			selectColor();
		}

		@Override
		public void mouseDown(MouseEvent e) {
		}

		@Override
		public void mouseUp(MouseEvent e) {
		}

	}

}